package com.news.web.servlet.admin;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.news.entity.User;
import com.news.service.UserService;
import com.news.service.impl.UserServiceImpl;

/**
 * Servlet implementation class AdminUserInsertServlet
 */
@WebServlet("/AdminUserInsertServlet")
public class AdminUserInsertServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// 设置编码方式
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		// 获取传入数据
		String userName = request.getParameter("userName");
		String userPass = request.getParameter("userPass");
		
		// 进行空值判断
		// 检查 所有值 是否为空
		if (userName == null || userPass == null) {
			// 处理 值为空的情况
			System.out.println("Str is null");
			request.setAttribute("userInsertBoolean", null); // 或者设定为null
			request.getRequestDispatcher("/admin/adminUser.jsp").forward(request, response);
			return; // 结束方法，避免继续执行下面的逻辑
		}
		// 实例化User
		User user = new User();
		user.setUserName(userName);
		user.setUserPass(userPass);
		// 创建UserService实例
		UserService userService = new UserServiceImpl();
		// 调用插入方法
		Boolean userInsertBoolean = userService.insertUser(user);
		// 将数据存入
		request.setAttribute("userInsertBoolean", userInsertBoolean);
		// 跳转回原页面
		request.getRequestDispatcher("/admin/adminUser.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
